// 烟花效果实现
class Firework {
    constructor(canvas) {
        this.canvas = canvas;
        this.ctx = canvas.getContext('2d');
        this.fireworks = []; // 烟花数组
        this.particles = []; // 烟花粒子数组
        this.autoLaunch = true; // 是否自动发射
        this.selectedColor = 'random'; // 选择的颜色
        this.lastLaunch = Date.now();
        this.launchInterval = 2000; // 自动发射间隔 (ms)
        
        this.resizeCanvas();
        this.bindEvents();
        this.animate();
    }
    
    // 调整Canvas大小
    resizeCanvas() {
        this.canvas.width = window.innerWidth;
        this.canvas.height = window.innerHeight;
    }
    
    // 绑定事件
    bindEvents() {
        window.addEventListener('resize', () => this.resizeCanvas());
        
        // 点击发射按钮
        document.getElementById('launchBtn').addEventListener('click', () => {
            this.launchFirework();
        });
        
        // 点击Canvas也可以发射
        this.canvas.addEventListener('click', (e) => {
            this.launchFireworkAt(e.clientX, e.clientY);
        });
        
        // 自动发射开关
        document.getElementById('autoLaunch').addEventListener('change', (e) => {
            this.autoLaunch = e.target.checked;
        });
        
        // 颜色选择
        document.querySelectorAll('.color-btn').forEach(btn => {
            btn.addEventListener('click', (e) => {
                this.selectedColor = e.target.getAttribute('data-color');
            });
        });
    }
    
    // 随机颜色
    getRandomColor() {
        if (this.selectedColor === 'random') {
            const colors = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#FF00FF', '#00FFFF', '#FFA500', '#FFFFFF'];
            return colors[Math.floor(Math.random() * colors.length)];
        } else {
            // 返回选定的颜色
            const colorMap = {
                'red': '#FF0000',
                'blue': '#0000FF',
                'green': '#00FF00',
                'gold': '#FFD700'
            };
            return colorMap[this.selectedColor] || '#FFFFFF';
        }
    }
    
    // 发射烟花
    launchFirework() {
        const startX = Math.random() * this.canvas.width;
        const startY = this.canvas.height;
        const endX = startX + (Math.random() * 100 - 50);
        const endY = Math.random() * this.canvas.height / 2;
        
        this.fireworks.push({
            startX,
            startY,
            endX,
            endY,
            x: startX,
            y: startY,
            color: this.getRandomColor(),
            speed: 2 + Math.random() * 3,
            angle: Math.atan2(endY - startY, endX - startX),
            alpha: 1,
            distanceTraveled: 0,
            distanceToTarget: Math.sqrt(Math.pow(endX - startX, 2) + Math.pow(endY - startY, 2)),
            particleCount: 50 + Math.floor(Math.random() * 50)
        });
        
        this.lastLaunch = Date.now();
    }
    
    // 在特定位置发射烟花
    launchFireworkAt(x, y) {
        const startX = Math.random() * this.canvas.width;
        const startY = this.canvas.height;
        
        this.fireworks.push({
            startX,
            startY,
            endX: x,
            endY: y,
            x: startX,
            y: startY,
            color: this.getRandomColor(),
            speed: 2 + Math.random() * 3,
            angle: Math.atan2(y - startY, x - startX),
            alpha: 1,
            distanceTraveled: 0,
            distanceToTarget: Math.sqrt(Math.pow(x - startX, 2) + Math.pow(y - startY, 2)),
            particleCount: 50 + Math.floor(Math.random() * 50)
        });
        
        this.lastLaunch = Date.now();
    }
    
    // 创建爆炸粒子
    createParticles(x, y, color, count) {
        for (let i = 0; i < count; i++) {
            const angle = Math.random() * Math.PI * 2;
            const speed = 1 + Math.random() * 3;
            
            this.particles.push({
                x,
                y,
                radius: 1 + Math.random(),
                color,
                speed,
                angle,
                friction: 0.95,
                gravity: 0.2,
                alpha: 1,
                decay: 0.015 + Math.random() * 0.015
            });
        }
    }
    
    // 更新烟花位置
    updateFireworks() {
        for (let i = this.fireworks.length - 1; i >= 0; i--) {
            const fw = this.fireworks[i];
            
            fw.x += Math.cos(fw.angle) * fw.speed;
            fw.y += Math.sin(fw.angle) * fw.speed;
            
            fw.distanceTraveled = Math.sqrt(Math.pow(fw.x - fw.startX, 2) + Math.pow(fw.y - fw.startY, 2));
            
            // 检查烟花是否到达目标位置
            if (fw.distanceTraveled >= fw.distanceToTarget) {
                this.createParticles(fw.x, fw.y, fw.color, fw.particleCount);
                this.fireworks.splice(i, 1);
            }
        }
    }
    
    // 更新粒子位置
    updateParticles() {
        for (let i = this.particles.length - 1; i >= 0; i--) {
            const p = this.particles[i];
            
            p.speed *= p.friction;
            p.x += Math.cos(p.angle) * p.speed;
            p.y += Math.sin(p.angle) * p.speed + p.gravity;
            p.alpha -= p.decay;
            
            if (p.alpha <= 0) {
                this.particles.splice(i, 1);
            }
        }
    }
    
    // 绘制烟花
    drawFireworks() {
        this.fireworks.forEach(fw => {
            this.ctx.beginPath();
            this.ctx.arc(fw.x, fw.y, 2, 0, Math.PI * 2);
            this.ctx.fillStyle = fw.color;
            this.ctx.fill();
        });
    }
    
    // 绘制粒子
    drawParticles() {
        this.particles.forEach(p => {
            this.ctx.globalAlpha = p.alpha;
            this.ctx.beginPath();
            this.ctx.arc(p.x, p.y, p.radius, 0, Math.PI * 2);
            this.ctx.fillStyle = p.color;
            this.ctx.fill();
        });
        this.ctx.globalAlpha = 1;
    }
    
    // 清除画布
    clear() {
        this.ctx.fillStyle = 'rgba(0, 0, 0, 0.2)';
        this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
    }
    
    // 动画循环
    animate() {
        requestAnimationFrame(() => this.animate());
        
        this.clear();
        this.updateFireworks();
        this.updateParticles();
        this.drawFireworks();
        this.drawParticles();
        
        // 自动发射
        if (this.autoLaunch && Date.now() - this.lastLaunch > this.launchInterval) {
            this.launchFirework();
        }
    }
}

// 当页面加载完成后初始化
document.addEventListener('DOMContentLoaded', () => {
    const canvas = document.getElementById('fireworks');
    new Firework(canvas);
}); 